import os
from JSONLanguageReader import JSONLanguageReader

__author__ = 'deadmoroz'


class LanguagesStorage(object):
    def __init__(self):
        self.__reader = JSONLanguageReader()
        self.__languages = {}

    def __load(self):
        languages = self.__reader.readAll()
        for language in languages:
            self.__languages[language.name] = language

    def get_supported_languages(self):
        if not self.__languages:
            self.__load()
        return self.__languages.keys()

    def __getitem__(self, name):
        if not self.__languages:
            self.__load()
        return self.__languages[name]


class StylesStorage(object):
    def __init__(self):
        self.__languages = {}

    def __load(self):
        for root, dirs, files in os.walk("styles"):
            for file in files:
                name, ext = os.path.splitext(file)
                if ext == ".css":
                    self.__languages[name] = open(os.path.join(root, file)).read()

    def get_supported_styles(self):
        if not self.__languages:
            self.__load()
        return self.__languages.keys()

    def __getitem__(self, name):
        if not self.__languages:
            self.__load()
        return self.__languages[name]

